//获取应用实例
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // adsall:[
    //   { name: '张此案', phone: '5212123332', ads: '陕西省西安市未央区', jtads: '凤城二路街道  海洋大厦1101'},
    //   { name: '张此案', phone: '5212123332', ads: '陕西省西安市未央区', jtads: '凤城二路街道  海洋大厦1101' },
    //   { name: '张此案', phone: '5212123332', ads: '陕西省西安市未央区', jtads: '凤城二路街道  海洋大厦1101' },
    //   { name: '张此案',  phone: '5212123332', ads: '陕西省西安市未央区', jtads: '凤城二路街道  海洋大厦1101' },   
    // ],
    edit:'编辑',
    ischange:true,
    chose: false,
    arr:[],
    
    address:[],
    current_type:1,
    currentPage:1,
    totalPage:1,
    isT:2
     
  },
  //编辑
  start_edit(){
    var type = this.data.current_type;
    var chose = this.data.chose;
    var edit = this.data.edit;
    console.log(chose)
    if (edit =='编辑'){
      this.setData({
        chose: true,
        edit: '完成'
      })
    }else{
      this.setData({
        chose: false,
        edit: '编辑'
      })
    } 
  },
  setDefault:function(e){
    var id = e.currentTarget.dataset.id;
    var type = this.data.current_type;
    var that = this;

    var param = {
      type: type,
      id: id
    };
    util.commonRequest('setDefaultAddress', 'post', param, function (res) {
      if (res.data.code == 200) {
        that.setData({
          address: res.data.data.data,
          totalPage: res.data.data.last_page
        })

      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
    var isT = that.data.isT;
    
    if(isT == 1)
    {
      wx.navigateBack();
    }
  },
  dalete(){
    var that = this;
    wx.showModal({
      title: '温馨提示:',
      content: '您确定删除该地址吗?删除后无法恢复',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          that.deleteFun();
        }
      },
    })
  },
  deleteFun:function(){

    var that = this;
    var adsall = that.data.address;
    var deleteStr = '';
    console.log(adsall)
    for (var i = 0; i < adsall.length; i++) {
      if (adsall[i].ischeck) {
        deleteStr += adsall[i]['id'] + ',';
      }
    };
    deleteStr = deleteStr.substring(0, deleteStr.length - 1);
    var that = this;
    var param = {
      id_str: deleteStr,
      type: that.data.current_type
    };
    util.commonRequest('deleteAddress', 'post', param, function (res) {
      if (res.data.code == 200) {
        that.setData({
          address: res.data.data.data,
          totalPage: res.data.data.last_page
        })
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1500
        })


      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },

  chose_opt(e){
    var index = e.currentTarget.dataset.index;
    var adsall = this.data.address;
    adsall[index].ischeck = !adsall[index].ischeck
    this.setData({
      address: adsall,
    })
  },
 //增加地址
  start_addedit(){
    var type = this.data.current_type;
    if(type==2){
      wx.navigateTo({
        url: '/pages/shop_receive_address/edit?type=2&id=0',
      })
      return;
    }
    app.navTo('../edit/edit?type='+type+'&id=0')
  },


  start_adsbj(e) {
    var id = e.currentTarget.dataset.id;
    var type = this.data.current_type;
    app.navTo('../edit/edit?type='+type+'&id='+id)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  initData:function(type){
    var that = this;
    var param = {
      type: type,
      currentPage: that.data.currentPage
    };
    
    util.commonRequest('addmoren', 'post', param, function (res) {
      if (res.data.code == 200) {
        var temp_total_page = res.data.data.last_page;

        var tempArr = res.data.data.data;
        var oriArr = that.data.address;
        var finalyArr = oriArr.concat(tempArr);

        that.setData({
          address: finalyArr,
          current_type: type,

          totalPage: temp_total_page
        })

      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  onLoad: function (options) {
    var type = options.type;// 0 收货 1 发货 2、我的收货地址(积分商城的)
     
    this.setData({
      current_type:type
    })
    
    if (type == 1){
      wx.setNavigationBarTitle({
        title: "我的发货地址"
      })
    } else{
      wx.setNavigationBarTitle({
        title: "我的收货地址"
      })
    }
    var isT = options.isT;
    if(isT){
      this.setData({
        isT: isT
      })
      
    }
  
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      address:[]
    })
    this.initData(this.data.current_type);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.onLoad({ type: this.data.current_type});
    } else {
      wx.showToast({
        'icon': 'loading',
        title: '已经是全部数据了',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})